package com.ocom.ugcapi.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.UgcApiUrl;
import com.ocom.common.constant.XFPayUrl;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.PerDiscountLimitRequest;
import com.ocom.common.request.web.ConsumeLimitConfigOnShowRequest;
import com.ocom.common.request.web.GetConsPerReportsRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.ugcapi.GzhUserInfo;
import com.ocom.ugcapi.client.HumanClientService;
import com.ocom.ugcapi.client.PayConfigClientService;
import com.ocom.ugcapi.client.XfRecodeClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2024年10月15日13:31:07
 **/
@Slf4j
@RestController
public class UserAuthController {

	@Autowired
	PayConfigClientService payConfigClientService;

	@Autowired
	HumanClientService humanClientService;

	@Autowired
	private XfRecodeClientService xfRecordFeign;

	//时区
	@PostMapping(UgcApiUrl.URL_timeZone)
	public Result timeZone(@RequestBody Map<String, Object> map) {
		GzhUserInfo user = (GzhUserInfo) map.get("userInfo");
		return payConfigClientService.getConsZoneList(user.getComId());
	}

	//消费限制
	@PostMapping(UgcApiUrl.URL_consumeLimit)
	public Result consumeLimit(@RequestBody Map<String, Object> map) {
		GzhUserInfo user = (GzhUserInfo) map.get("userInfo");

		Result<EntityXfAccountInfo> result = payConfigClientService.GetXFAccount(user.getComId(), user.getPId().intValue(), 0);

		if (result.getCode() != 200) {
			return result;
		}

		Result<EntityXfAccountType> entityXfAccountTypeResult = payConfigClientService.GetXFAccountTypeDetails(user.getComId(), result.getData().getAccTypeId().intValue());

		if (result.getCode() != 200) {
			return entityXfAccountTypeResult;
		}

		if (entityXfAccountTypeResult.getData().getGrouping() <= 0) {
			return ResultUtil.error(ResultEnum.Err_3104);
		}

		ConsumeLimitConfigOnShowRequest request = JSON.parseObject(JSON.toJSONString(map), ConsumeLimitConfigOnShowRequest.class);
		Long comId = request.getUserInfo().getComId();
		request.setComId(comId);
		request.setGrouping(entityXfAccountTypeResult.getData().getGrouping());
		request.setLimitField(0);
		return payConfigClientService.getConsumeLimitConfig(request);
	}

	//公众号支付（/pay）
	@PostMapping(UgcApiUrl.URL_modifyUserAccType)
	public Result modifyUserAccType(@RequestBody Map<String, Object> map) {
		GzhUserInfo user = (GzhUserInfo) map.get("userInfo");

		Result<EntityXfAccountInfo> result = payConfigClientService.GetXFAccount(user.getComId(), user.getPId().intValue(), 0);

		if (result.getCode() != 200) {
			return result;
		}

		Result<EntityXfAccountType> entityXfAccountTypeResult = payConfigClientService.GetXFAccountTypeDetails(user.getComId(), result.getData().getAccTypeId().intValue());

		if (result.getCode() != 200) {
			return result;
		}

		if (entityXfAccountTypeResult.getData().getGrouping() <= 0) {
			return ResultUtil.error(ResultEnum.Err_3104);
		}

		Result<List<EntityXfAccountType>> entityXfAccountTypeList = payConfigClientService.GetXFAccountType(user.getComId(), entityXfAccountTypeResult.getData().getGrouping());

		List<Integer> accTypes = entityXfAccountTypeList.getData().stream().map(EntityXfAccountType::getAccTypeId).collect(Collectors.toList());

		Integer accTypeId = (Integer) map.get("accTypeId");

		if (!accTypes.contains(accTypeId)) {
			return ResultUtil.error(ResultEnum.Err_4023);
		}

		if (result.getData().getAccTypeId().equals(accTypeId)) {
			return ResultUtil.error(ResultEnum.Err_811);
		}

		result.getData().setAccTypeId(accTypeId);

		return payConfigClientService.UpdateXFAccount(result.getData());
	}


	/**
	 * @return com.ocom.common.resp.Result
	 * @Description:人员消费汇总报表
	 * @author lu
	 * @date 2023/2/17 14:40
	 */
	@PostMapping(XFPayUrl.URL_ConsPerReportsonShow)
	public Result consPerReportsonShow(@RequestBody Map<String, Object> map) {
		GzhUserInfo user = (GzhUserInfo) map.get("userInfo");

		GetConsPerReportsRequest request = new GetConsPerReportsRequest();

		request.setComId(user.getComId());
		request.setPId(user.getPId());
		request.setDeptIds("-1");
		request.setStartTime(DateUtils.getFirstDate("yyyy-MM-dd 00:00:00"));
		request.setEndTime(DateUtil.getDateTime());
		request.setPage(1);
		request.setSize(50);

		return xfRecordFeign.getConsPerReports(request);
	}

	//公众号支付（/pay）
	@PostMapping(UgcApiUrl.URL_getPerDiscountBalance)
	public Result getPerDiscountBalance(@RequestBody Map<String, Object> map) {
		GzhUserInfo user = (GzhUserInfo) map.get("userInfo");
		PerDiscountLimitRequest request = new PerDiscountLimitRequest();
		request.setComId(user.getComId());
		request.setPId(user.getPId());
		return payConfigClientService.perDiscountLimit(request);
	}


}